package com.fanketly.funacg2.ui.viewmodels

import android.util.Log
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.cachedIn
import com.fanketly.funacg2.data.TAG
import com.fanketly.funacg2.data.model.HistoryModel
import com.fanketly.funacg2.data.paging.HistoryPagingSource
import com.fanketly.funacg2.data.repo.HistoryRepo
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class HistoryViewModel @Inject constructor(private val repo: HistoryRepo) : ViewModel() {
    init {
        Log.d(TAG, "HistoryViewModel_init ")
    }

    val pager: Flow<PagingData<HistoryModel>> = Pager(
        PagingConfig(
            pageSize = 20, initialLoadSize = 20, enablePlaceholders = true, maxSize = 200
        )
    ) {
        HistoryPagingSource(repo)
    }.flow.cachedIn(viewModelScope)


    fun delete(id: Int,onEnd:()->Unit) {
        viewModelScope.launch {
            repo.delete(id)
            onEnd()
        }
    }
}